package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Dish;
import com.itheima.bean.PageParam;
import com.itheima.bean.R;
import com.itheima.dto.DishDto;
import com.itheima.service.DishService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品的controller
 */
@Api(tags = "菜品接口") //为swagger doc文档写别名，中文
@RestController
public class DishController {

    @Autowired
    private DishService ds;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping("/dish")
    public R add(@RequestBody DishDto dishDto){
        System.out.println("dishDto = " + dishDto);

        //1、调用service
        int row = ds.add(dishDto);
        //2、响应回去
        if (row > 0){
            return R.success("新增菜品成功");
        } else {
            return R.error("新增菜品失败");
        }
    }

    /**
     * 分页查询
     * @param param
     * @return
     */
    @GetMapping("/dish/{param}")
    public R findPage(@PathVariable String param){
        //1、转化成JavaBean
        PageParam pageParam = JSON.parseObject(param, PageParam.class);

        //2.交代service干活
        IPage<DishDto> page = ds.findPage(pageParam);

        //3、响应
        return R.success(page);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping("/dish")
    public R update(@RequestBody  DishDto dishDto){
        System.out.println("dishDto = " + dishDto);

        //1. 调用service
        int row = ds.update(dishDto);

        //2. 响应回去
        if(row >0 ){
            return R.success("修改菜品成功");
        }else{
            return R.error("修改菜品失败");
        }
    }

    /**
     * 按照菜品的分类来查找菜品数据
     * @param categoryId
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "categoryId",value = "分类id"),
                        @ApiImplicitParam(name = "status",value = "状态")})
    @ApiOperation(value = "查询菜品数据") //doc文档
    @GetMapping("/dish/list")
    public R findByCategoryId(Long categoryId,int status){
        //1、调用service
        //List<Dish> list = ds.findByCategoryId(categoryId,status);

        //因为前端要显示，选择规格入口，这个只有菜品有口味数据的时候，才会出现
        //所以现在查询的菜品数据，除了它本身的数据之外，还要拥有口味的数据
        List<DishDto> list = ds.findByCategoryId(categoryId,status);
        return R.success(list);
    }


}
